package com.atomgit.ai.proxy.mapper;

import com.atomgit.ai.proxy.entity.UserApiKey;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.util.List;

@Mapper
public interface UserApiKeyMapper extends BaseMapper<UserApiKey>{

    @Update("update user_api_key set api_key=#{apiKey},custom_model_name=#{customModelName} where model_id=#{modelId} and target_type=#{targetType} and target_id=#{targetId} and deleted = 0")
    void updateApiKeyByModelIdAndTargetIdAndTargetTypeAndVersion(String apiKey, String modelId,Integer targetType, String targetId,String customModelName);

    @Update("update user_api_key set api_key=#{apiKey},custom_model_name=#{customModelName} where model_id=#{modelId} and deleted = 0")
    void updateApiKeyByModelIdAndVersion(String apiKey,String modelId,String customModelName);

    @Select("select * from user_api_key where model_id=#{modelId} and user_id=#{userId} and deleted = 0")
    UserApiKey selectByModelIdAndUserId(String modelId, String userId);

    @Select("select * from user_api_key where model_id=#{modelId} and target_id=#{targetId} and version = #{version} and target_type=#{targetType}   and deleted = 0")
    UserApiKey selectByModelIdAndTargetIdAndTargetTypeAndVersion(String modelId, String targetId,String version,Integer targetType);

    @Select("select * from user_api_key where model_id=#{modelId} and version = #{version} and deleted = 0")
    UserApiKey selectByModelIdAndVersion(String modelId,String version);

    @Select("select * from user_api_key where model_id=#{modelId} and user_id=#{userId} and deleted = 0")
    List<UserApiKey> selectListByModelIdAndUserId(String modelId, String userId);

    @Select("select * from user_api_key where target_type=#{targetType} and target_id=#{targetId} and deleted = 0")
    List<UserApiKey> selectByTargetTypeAndTargetId(Integer targetType, String targetId);

    @Select("select * from user_api_key where and deleted = 0")
    List<UserApiKey> selectAll();
}
